package com.qf.service.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.common.bean.Pages;
import com.qf.common.dto.AreaDto;
import com.qf.common.mapper.QfAreaMapper;
import com.qf.common.model.QfArea;
import com.qf.service.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 千锋健哥
 */
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private QfAreaMapper areaMapper;

    @Override
    public QfArea add(QfArea type) {
        type.setCreateTime(new Date());
        type.setUpdateTime(new Date());
        areaMapper.insert(type);
        return type;
    }

    @Override
    public boolean delete(String id) {
        LambdaQueryWrapper<QfArea> queryWrapper = Wrappers.<QfArea>query().lambda();
        queryWrapper.eq(id != null, QfArea::getCode, id);
        int count = areaMapper.delete(queryWrapper);
        return count > 0;
    }

    @Override
    public QfArea update(QfArea type) {
        type.setUpdateTime(new Date());

        LambdaQueryWrapper<QfArea> queryWrapper = Wrappers.<QfArea>query().lambda();
        queryWrapper.eq(type.getCode() != null, QfArea::getCode, type.getCode());

        areaMapper.update(type, queryWrapper);
        return type;
    }

    @Override
    public QfArea selectById(String id) {
        LambdaQueryWrapper<QfArea> queryWrapper = Wrappers.<QfArea>query().lambda();
        queryWrapper.eq(id != null, QfArea::getCode, id);
        QfArea result = areaMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public Page<QfArea> list(AreaDto dto) {
        if (ObjectUtil.isNull(dto)) {
            dto = new AreaDto();
            dto.setPage(new Pages());
        }

        if (ObjectUtil.isNull(dto.getPage())) {
            dto.setPage(new Pages());
        }

        Page<QfArea> page = areaMapper.selectPage(dto.getPage().convert(), fuzzyQuery(dto));
        return page;
    }

    @Override
    public List<QfArea> all(AreaDto dto) {
        List<QfArea> list = areaMapper.selectList(fuzzyQuery(dto));
        return list;
    }

    @Override
    public LambdaQueryWrapper<QfArea> fuzzyQuery(AreaDto dto) {
        LambdaQueryWrapper<QfArea> queryWrapper = Wrappers.<QfArea>query().lambda();
        if (ObjectUtil.isNotEmpty(dto)) {
            //根据行政区编码模糊查询
            queryWrapper.like(dto.getCode() != null, QfArea::getCode, dto.getCode());
            //根据行政区名称模糊查询
            queryWrapper.like(StringUtils.isNotEmpty(dto.getName()), QfArea::getName, dto.getName());
            //根据等级精确查询
            queryWrapper.eq(dto.getLevel() != null, QfArea::getLevel, dto.getLevel());
            //根据类型精确查询
            queryWrapper.eq(dto.getType() != null, QfArea::getType, dto.getType());
            //根据行政区简称精确查询
            queryWrapper.eq(StringUtils.isNotEmpty(dto.getAbname()), QfArea::getAbname, dto.getAbname());
            //根据父级行政区编码精确查询
            queryWrapper.eq(dto.getPid() != null, QfArea::getPid, dto.getPid());
        }
        return queryWrapper;
    }

}
